<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="设备列表" prop="deviceNo">
                <el-select v-model="ruleForm.deviceNo" class="w100p" clearable filterable>
                    <el-option v-for="(item, index) in deviceList" :key="index" :label="item.deviceName"
                        :value="item.deviceNo" />
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import distributionEquipmentForBreedingFacilities from '@/api/basicInformation/distributionEquipmentForBreedingFacilities';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        deviceList: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: distributionEquipmentForBreedingFacilities.addlist,
            ruleForm: {
                item1: []
            },
            rules: {
                deviceNo: [
                    { required: true, message: '请选择设备', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    
    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.ruleForm = { ...this.data };
                this.addEditApi = distributionEquipmentForBreedingFacilities.addlist;
                this.addEditName = '添加';
            }
        },
        //提交表单
        submitForm() {
            console.log(this.ruleForm)
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        this.$message.warning('添加异常/重复添加')
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            setTimeout(() => {
                this.ruleForm = {};
                this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
            }, 100)
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  